// pages/order/index/index.js
let util = require('../../../utils/index')
import http from '../../../services/http/index'
import {
  ListData
} from '../../../utils/Data'
let allTabImgs = ['../../../images/orders/airportshuttle.png', '../../../images/orders/airportshuttle_ac.png', '../../../images/orders/travel.png', '../../../images/orders/travel_ac.png', '../../../images/orders/bus.png', '../../../images/orders/bus_ac.png', '../../../images/orders/reservation.png', '../../../images/orders/reservation_ac.png']
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabImgs: [{
        label: '机场接送',
        type: 0,
        img: allTabImgs[1]
      },
      {
        label: '旅游包车',
        type: 1,
        img: allTabImgs[2]
      },
      {
        label: '结伴班车',
        type: 2,
        img: allTabImgs[4]
      },
      {
        label: '预约用车',
        type: 3,
        img: allTabImgs[6]
      }
    ],
    cindex: 0,
    date: '',
    airList: {
      data: [],
      hasMore:true,
      page:1
    }, //机场接送数据
    appointCarsList: {
      data: [],
      hasMore:true,
      page:1
    }, //预约用车数据
    busList: {
      data: [],
      hasMore:true,
      page:1
    },
    chartersList: {
      data: [],
      hasMore:true,
      page:1
    }, //包车数据
    orderFilters: [{
      label: '全部',
      id: ''
    }],
    cfilterIndex: 0, //筛选状态id
    filterHide: true,
    keyword:'',
    airItemImgUrl: ['../../../images/orders/jieji.png', '../../../images/orders/songji.png'],
    monthTransfer: 0 //本月转单次数
  },
  //tab切换
  chooseType: function(e) {
    var type = e.currentTarget.dataset.type;
    var tabImgs = this.data.tabImgs;
    var initIndex = [0, 2, 4, 6];
    initIndex[type]++;
    tabImgs.forEach(function(el, i) {
      el.img = allTabImgs[initIndex[i]]
    });
    this.setData({
      tabImgs: tabImgs,
      cindex: type
    });
    this.clearCurrentList();
    this.getList();
  },
  clearCurrentList(){
    let type = this.data.cindex;
    switch (type){
      case 0:{
        this.setData({
          airList: {
            data: [],
            hasMore:true,
            page:1
          }
        });
        break;
      }
      case 1:{
        this.setData({
          chartersList: {
            data: [],
            hasMore:true,
            page:1
          }
        });
        break;
      }
      case 2:{
        this.setData({
          busList: {
            data: [],
            hasMore:true,
            page:1
          }
        });
        break;
      }
      case 3:{
        this.setData({
          appointCarsList: {
            data: [],
            hasMore:true,
            page:1
          }
        });
        break;
      }
    }
  },
  // 过滤选择
  chooseFilter(e) {
    let type = e.currentTarget.dataset['type'];
    this.setData({
      cfilterIndex: type,
      filterHide: true
    });
    this.clearCurrentList();
    this.getList()
  },
  showFilter() {
    this.setData({
      filterHide: false
    })
  },
  //获取列表
  getList(e) {
    var keyword = this.data.keyword;
    if(e){
      keyword = e.detail.value;
      this.setData({
        keyword:keyword
      })
    }
    let param = {
      date: this.data.date,
      status: this.data.orderFilters[this.data.cfilterIndex].id
    };
    if(keyword!==''){
      param.keyword = keyword
    };
    var type = this.data.cindex;
    var arrName = '';
    if(type == 0){
      arrName = 'airList'
    }else if(type == 1){
      arrName = 'chartersList'
    }else if(type == 2){
      arrName = 'busList'
    }else if(type == 3){
      arrName = 'appointCarsList'
    };
    if(!this.data[arrName].hasMore)return false;
    param.page = this.data[arrName].page;
    wx.showToast({
      title: 'loading',
      icon: 'loading',
      duration: 10000,
      mask: true
    });
    //成功回调 列表公共处理方法的提取
    var succCall = function(data, _this) {
      wx.hideToast();
      var status = [{
        label: '全部',
        id: ''
      }];
      for (var key in data.statusList) {
        status.push({
          id: key,
          label: data.statusList[key]
        });
      };
      _this.setData({
        'monthTransfer': data.monthTransfer || 0,
        'orderFilters': status
      });
    };

    //接机单
    if (type == 0) {
      //接机
      http.myMission(param) //是this.data.date 测试先不传
        .then(data => {
          console.log(data)
          succCall(data, this);
          var hasMore = false,page=param.page;
          if(data.next_page){
            //有下一页
            hasMore = true;
            page++;
          }else{
            hasMore = false
          };
          var oldData = this.data.airList.data;
          var newData = oldData.concat(data.list);
          this.setData({
            'airList': Object.assign(new ListData(), {
              data: newData,
              hasMore:hasMore,
              page:page
            })
          });
        });
    }

    if (type == 3) {
      //预约用车
      http.myAppointCarList(param)
        .then(data => {
          succCall(data, this);
          var hasMore = false,page=param.page;
          if(data.next_page){
            //有下一页
            hasMore = true;
            page++;
          }else{
            hasMore = false
          };
          var oldData = this.data.appointCarsList.data;
          var newData = oldData.concat(data.lists);
          this.setData({
            'appointCarsList': Object.assign(new ListData(), {
              data: newData,
              hasMore:hasMore,
              page:page
            })
          });
        });
    }

    if (type == 2) {
      //班车
      http.myBusList(param)
        .then(data => {
          succCall(data, this);
          var hasMore = false,page=param.page;
          if(data.next_page){
            //有下一页
            hasMore = true;
            page++;
          }else{
            hasMore = false
          };
          var oldData = this.data.busList.data;
          var newData = oldData.concat(data.list);
          this.setData({
            'busList': Object.assign(new ListData(), {
              data: newData,
              hasMore:hasMore,
              page:page
            })
          });
        });
    }

    if (type == 1) {
      //包车
      http.myCharterList(param)
        .then(data => {
          succCall(data, this);
          var hasMore = false,page=param.page;
          if(data.next_page){
            //有下一页
            hasMore = true;
            page++;
          }else{
            hasMore = false
          };
          var oldData = this.data.chartersList.data;
          var newData = oldData.concat(data.list);
          this.setData({
            'chartersList': Object.assign(new ListData(), {
              data: newData,
              hasMore:hasMore,
              page:page
            })
          });
        })
    }
  },
  next(e) {
    let id = e.currentTarget.dataset['id'];
    console.log(`../detail/detail?type=${this.data.cindex}&id=` + id)
    util.default.go(`../detail/detail?type=${this.data.cindex}&id=` + id);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      'date': options.date
    });
    wx.setNavigationBarTitle({
      title: options.date
    })
    this.getList();
  },

  //跳转发消息
  sendMessage(e) {
    let id = e.currentTarget.dataset['id'];
    util.default.go('../../my/message/messageChart?id=' + id);
  },

  // 打电话：
  callPhone(e) {
    let phoneNumber = e.currentTarget.dataset['telphone'];
    if (!phoneNumber) {
      wx.showToast({
        icon: 'none',
        title: '该订单没有提供电话号码'
      })
      return false;
    }
    wx.makePhoneCall({
      phoneNumber: phoneNumber,
      success: function() {
        console.log("拨打电话成功！")
      },
      fail: function() {
        console.log("拨打电话失败！")
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.getList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})